//
//  SetViewController.m
//  舟航007机车卫士
//
//  Created by admin on 15/11/9.
//  Copyright (c) 2015年 redoor. All rights reserved.
//

#import "SetViewController.h"
#import "PopLoadMsg.h"
#import "PopMsg.h"
#import "LoginViewController.h"
#import "LoginViewController.h"
#import "AlertView.h"
#import "InsuranceRulesController.h"
#import "InstructionsController.h"
@interface SetViewController ()
{
    NSArray *_dataList;
    NSArray *_ctrlArray;
    UIButton *_logoutBtn;
}

@property (nonatomic, strong) NSString *userOrTourist;

@end

@implementation SetViewController
- (instancetype)init {
    self = [super init];
    if (self) {
        _dataList = @[@[@"car",@"personaldata",@"privacprotection",@"changepassword",@"serviceconter",@"使用说明",@"保险条例",@"aboutus",@"aboutus"],
                      @[@"我的车辆",@"个人资料",@"隐私保护",@"修改密码",@"服务中心",@"使用说明",@"保险条例",@"关于我们"]];
        _ctrlArray = @[@"MyCarViewController",
                       @"PersonalDataViewController",
                       @"PrivacProtectViewController",
                       @"ChangePwdViewController",
                       @"ServiceCenterViewController",
                       @"InstructionsController",
                       @"InsuranceRulesController",
                       @"AboutUsViewController"
                     ];
    }
    return self;
}

- (void)dealloc
{
    NSLog(@"释放对象%@",[self class]);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.userOrTourist = [[NSUserDefaults standardUserDefaults] objectForKey:@"userOrTourist"];
    [self configView];
}

- (void)configView
{
    self.view.backgroundColor=[UIColor colorWithRed:(CGFloat)45/255 green:(CGFloat)143/255 blue:(CGFloat)247/255 alpha:1.0];
    UILabel *titletext=[[UILabel alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-180*W)/2,20+10*H, 180*W, 20*H)];
    titletext.textColor=[UIColor whiteColor];
    titletext.textAlignment=NSTextAlignmentCenter;
    titletext.text=@"设置";
    [self.view addSubview:titletext];
    [self createBtns];
    _logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _logoutBtn.frame = CGRectMake((SCREEN_WIDTH-104*W)/2, 70*H+(40*H)*8+40*H, 104*W, 34*H);
    [_logoutBtn setBackgroundImage:[UIImage imageNamed:@"logout.png"] forState:UIControlStateNormal];
    [_logoutBtn setBackgroundImage:[UIImage imageNamed:@"logout_on.png"] forState:UIControlStateHighlighted];
    [_logoutBtn addTarget:self action:@selector(logoutBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_logoutBtn];
}

- (void)createBtns
{
    for (int i=0; i<8; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(30*W, 70*H+(40*H)*i, SCREEN_WIDTH-60*W, 40*H);
        UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (CGRectGetHeight(btn.frame)-15*H)/2, 15*H, 15*H)];
        if (i==0 || i==2) {
            leftImageView.frame = CGRectMake(0, (CGRectGetHeight(btn.frame)-10*H)/2, 15*W, 10*H);
        }
        [leftImageView setImage:[UIImage imageNamed:_dataList[0][i]]];
        [btn addSubview:leftImageView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40*W, (CGRectGetHeight(btn.frame)-30*H)/2, 150*W, 30*H)];
        titleLabel.text = _dataList[1][i];
        titleLabel.font = [UIFont systemFontOfSize:16];
        titleLabel.textColor = [UIColor whiteColor];
        [btn addSubview:titleLabel];
        
        //NSLog(@"ww222wwww  %@",_dataList);
        
        UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(btn.frame)-8*W, (CGRectGetHeight(btn.frame)-14.5*H)/2, 8*W, 14.5*H)];
        [rightImageView setImage:[UIImage imageNamed:@"back_right"]];
        [btn addSubview:rightImageView];
        
        UILabel *separateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(btn.frame)-1, CGRectGetWidth(btn.frame), 1)];
        separateLabel.backgroundColor = [UIColor cyanColor];
        [btn addSubview:separateLabel];
        
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
}

#pragma mark - 网络请求
//退出登录请求
- (void)logoutRequest
{
    PopLoadMsg *loadmsg = [PopLoadMsg new];
    //加载效果操作
    [loadmsg delayLoadImages];
    
    NSDictionary *paramdic = @{@"loginId":[[NSUserDefaults standardUserDefaults] objectForKey:@"phonenumber"]};
    NSString *url = [urlstring stringByAppendingString:@"logout.do"];
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:url withParam:paramdic success:^(AFHTTPRequestOperation *op,id responseObject){
        [loadmsg delayImagesRemove];
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSLog(@"dic = %@", dic);
        
        NSInteger flag = [[dic objectForKey:@"flag"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        if (flag == 0) {
            NSLog(@"Logout Success!");
            [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"judgeNo"];
//            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"phonenumber"];
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"password"];
            [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"logout"];
            self.view.window.rootViewController=[LoginViewController new];
        }
        else {
            [PopMsg showMessage:msg Lasttime:1.0f];
        }
        
    }failure:^(AFHTTPRequestOperation *op,NSError *error){
        [loadmsg delayImagesRemove];
        [PopMsg showMessage:@"网络异常，请重试" Lasttime:1.0f];
    }];
}

#pragma mark - 点击事件
- (void)btnClicked:(UIButton *)btn
{
    NSInteger index = btn.tag - 100;
    NSString *className = _ctrlArray[index];
    Class cls = NSClassFromString(className);
    UIViewController *vc = [[cls alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)logoutBtnClicked
{
    if ([self.userOrTourist isEqualToString:@"1"]) {
        [self logoutRequest];
    }
    else {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        [self presentViewController:loginVC animated:YES completion:nil];
    }
}

#pragma mark -
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden=YES;
    NSDictionary *dic = @{@"key":@"3"};
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changetabbarone" object:nil userInfo:dic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
